function EventHandler(){
	this.target = arguments[0];
	
	//Event targets (elements whose events are listened to)
	if (arguments[1] instanceof Array){
	    this.targets=arguments[1];	
	} else {
        this.targets=new Array();
	    for (var i=1; i<arguments.length; i++){
		    this.targets.push(arguments[i]);
	    }
	}
		
	//Event listeners
	this.listeners=new Object();
	
	this.handlers.push(this);
	this.handlerIndex=this.handlers.length-1;	
	
	Framework.instance.registerFinalizer(this);		
}

EventHandler.prototype.handlers=new Array();

EventHandler.prototype.registerListener=function (eventName, listener){

	if (typeof(this.listeners[eventName])=="undefined"){
		//No listeners for this event yet
		this.listeners[eventName]=new Array();
		this["on"+eventName]=this.createListenerInvoker(eventName); 
		var handler=this.createHandler(this, eventName);
		for (var i=0; i<this.targets.length; i++){
			this.hookUpListener(this.targets[i], eventName, handler);
		}
	}
	
	this.listeners[eventName].unshift(listener);
	
}

EventHandler.prototype.createListenerInvoker=function (eventName) {
		var funcBody="for (var i=0; i<this.listeners['"+eventName+"'].length; i++){ "
					+"if (this.listeners['"+eventName+"'][i].on"+eventName+"(arguments.callee.target)==false) return; "
					+"} ";
		var func = new Function(funcBody);
		func.target = this.target;
		return func;
}		

//Hooks up handler to element
EventHandler.prototype.hookUpListener=function (element, eventName, handler) {
	if (element != null && element.attachEvent){	
		element.attachEvent("on"+eventName, handler);
	}
	if (element != null && element.addEventListener) {
		element.addEventListener(eventName, handler, true);
	}
}
	
//Creates handler from listener object
EventHandler.prototype.createHandler=function (listener, eventName) {
	//Not used for new-manner event handling
	//this.handlers.push(listener);
	//var handlerIndex=this.handlers.length-1;
	var handlerFuncBody="var eventListener=EventHandler.prototype.handlers["+this.handlerIndex+"];"
						+"eventListener.on"+eventName+"(EventHandler.getEvent(nnevent));";
	
    return new Function("nnevent", handlerFuncBody);
}

EventHandler.prototype.finalize = function () {
    this.target = null;
    this.targets.removeAll();
    this.listeners = null;
    this.handlers.removeAll();        
}

EventHandler.getEvent = function (nnevent) {
	return nnevent?nnevent:event;
}

EventHandler.getTarget = function (eventObj) {
    return eventObj.srcElement?eventObj.srcElement:eventObj.target;    
}